package geguol.cc.widgetapplication.widget;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import java.util.List;

import geguol.cc.widgetapplication.R;

/**
 * 作者  格锅
 * 时间  2017/3/1 16:28
 * 文件  WidgetApplication
 * 描述  searchView + popwindow
 * 将请求网络数据的方法暴露
 * 数据封装自己处理
 */

public class  SearchDropListView extends LinearLayout{
	private PopupWindow popupWindow;
	private ListView listView;
	private EditText textView;
	private android.support.v7.widget.SearchView searchView;
	private Activity mContext;
	private float alpha=0.75f;//背景透明度

	public SearchDropListView(Context context) {
		this(context,null);
	}

	public SearchDropListView(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}
	public SearchDropListView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView();
	}

	public void setRequestInterface(RequestInterface requestInterface) {
		this.requestInterface = requestInterface;
	}

	private RequestInterface requestInterface;
	public interface RequestInterface<E> {
		/**
		 * @param keywords
		 * 实现通过关键字 搜索的逻辑
		 * @see #showResult(List) 传入搜索的结果
		 */
		void onRequest(String keywords);

		/**
		 * @param object
		 * 从结果中选择 一项
		 */
		void onResponse(E object);
	}

	public interface SearchBean {
		String getText();//需要展示的 文本
	}

	private void initView() {
		searchView = new android.support.v7.widget.SearchView(getContext());
		addView(searchView,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		textView = (EditText) searchView.findViewById(R.id.search_src_text);
		searchView.setIconifiedByDefault(false);//默认展开
		searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
			@Override
			public boolean onQueryTextSubmit(String query) {
				if (requestInterface!=null)requestInterface.onRequest(query);
				return false;
			}

			@Override
			public boolean onQueryTextChange(String newText) {
				return false;
			}
		});

	}

	/**
	 * @param result 查询的结果
	 */
	public void  showResult(List<? extends SearchBean> result){
			showPopWindow(result);
	}


	/**
	 * 打开下拉列表弹窗
	 * @param result
	 */
	private void showPopWindow(List result) {
		if (listView==null) {
			listView = new ListView(getContext());
			LinearLayout.LayoutParams pl = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			listView.setLayoutParams(pl);
		}
		listView.setAdapter(new DropListAdapter(getContext(),result));
		popupWindow = new PopupWindow(listView,searchView.getWidth(),searchView.getHeight()*8);
		popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_popwindow));//这个背景色是给弹出区域添加的颜色；并不是正规屏幕
		popupWindow.setOutsideTouchable(true);
		popupWindow.setAnimationStyle(R.style.TRE_ANIM_STYLE);
		popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				setBackgroundAlpha(alpha,1f,300);
			}
		});
		popupWindow.showAsDropDown(this,getPaddingLeft(),0);
		setBackgroundAlpha(1f,alpha,240);
	}
	private void closePopWindow(){
		popupWindow.dismiss();
		popupWindow = null;
	}
	private void setBackgroundAlpha(float from,float to,int duration){
		if (mContext==null)return;
		final WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
		ValueAnimator animator = ValueAnimator.ofFloat(from,to);
		animator.setDuration(duration);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				lp.alpha = (float) animation.getAnimatedValue();
				mContext.getWindow().setAttributes(lp);
			}
		});
		animator.start();
	}

	/**
	 * @param context
	 *  设置背景变暗
	 */
	public void dimBackground(Activity context){
		mContext = context;
	}

	class DropListAdapter extends BaseAdapter {
		Context mContext;
		List<? extends SearchBean> mData;
		LayoutInflater inflater;
		DropListAdapter(Context ctx, List<? extends SearchBean> data){
			mContext  = ctx;
			mData = data;
			inflater = LayoutInflater.from(mContext);
		}
		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ListItemView listItemView = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.dropdown_list_item, null);
				listItemView = new ListItemView();
				listItemView.tv = (TextView) convertView.findViewById(R.id.tv);
				listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
				convertView.setTag(listItemView);
			} else {
				listItemView = (ListItemView) convertView.getTag();
			}
			final SearchBean bean = mData.get(position);
			listItemView.tv.setText(bean.getText());
			listItemView.layout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (requestInterface!=null) requestInterface.onResponse(mData.get(position));
					textView.setText(bean.getText());
					textView.setSelection(bean.getText().length());
					closePopWindow();
				}
			});
			return convertView;
		}

	}
	private static class ListItemView{
		TextView tv;
		LinearLayout layout;
	}

}
